package com.cssnj.portal.service.admin.editfunction;

import com.cssnj.portal.domain.common.GeneralConstants;
import com.cssnj.portal.utils.PinYinUtils;
import com.cssnj.portal.domain.func.GlGnEntity;
import com.cssnj.portal.domain.func.GlGnRepository;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.UUID;

@Service("editfunctionService")
public class EditfunctionService extends BaseService {

    @Autowired
    private GlGnRepository glGnRepository;

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        if ("updateWjj".equals(jsonRequest.getHandleCode())) {
            return updateWjj(jsonRequest);
        } else if ("addWjj".equals(jsonRequest.getHandleCode())) {
            return addWjj(jsonRequest);
        } else if ("updateCdxx".equals(jsonRequest.getHandleCode())) {
            return updateCdxx(jsonRequest);
        } else if ("updateGnLj".equals(jsonRequest.getHandleCode())) {
            return updateGnLj(jsonRequest);
        }
        return null;
    }

    //新增文件夹
    @Transactional
    public JsonResponse addWjj(JsonRequest jsonRequest) throws SQLException {
        //默认以国税视角
        //查询国税岗位分类列表
        JsonResponse jsonResponse = new JsonResponse();
        String pid = (String) jsonRequest.getReqData().get("pid");
        String mc = (String) jsonRequest.getReqData().get("mc");
        String zt = (String) jsonRequest.getReqData().get("zt");
        String xh = (String) jsonRequest.getReqData().get("xh");
        String mc_jc = PinYinUtils.getPinYinHeadChar(mc);
        String uuid = UUID.randomUUID().toString();
        GlGnEntity vo = new GlGnEntity();
        vo.setGnId(uuid);
        vo.setGnMc(mc);
        vo.setXyBj(zt);
        vo.setPx(Long.parseLong(xh));
        vo.setGnjc(mc_jc);
        vo.setOpenType("0");
        vo.setSjgnId(pid);
        vo.setBeKjfs("1");
        vo.setXtDm(GeneralConstants.XT_DM);
        vo.setKzBj("1");
        vo.setXsBj("1");
        glGnRepository.save(vo);
        jsonResponse.getRepData().put("id", uuid);
        jsonResponse.getRepData().put("xh", xh);
        return jsonResponse;
    }

    //修改文件夹
    @Transactional
    public JsonResponse updateWjj(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        // 修改文件夹基本信息
        String id = (String) jsonRequest.getReqData().get("id");
        String mc = (String) jsonRequest.getReqData().get("mc");
        String zt = (String) jsonRequest.getReqData().get("zt");
        String mc_jc = PinYinUtils.getPinYinHeadChar(mc);
        glGnRepository.updateWjj(id, mc, mc_jc, zt);
        // 如果位置有修改，修改位置
        String wjjwz = (String) jsonRequest.getReqData().get("wjjwz");
        if (null != wjjwz && !"".equals(wjjwz)) {
            glGnRepository.updatePidById(id, wjjwz);
        }
        return jsonResponse;
    }

    //修改菜单信息
    @Transactional
    public JsonResponse updateCdxx(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        // 修改菜单基本信息
        String id = (String) jsonRequest.getReqData().get("id");
        String mc = (String) jsonRequest.getReqData().get("mc");
        String zt = (String) jsonRequest.getReqData().get("zt");
        String dkfs = (String) jsonRequest.getReqData().get("dkfs");
        String mc_jc = PinYinUtils.getPinYinHeadChar(mc);
        glGnRepository.updateCdxx(id, mc, mc_jc, zt, dkfs);
        // 如果位置有修改，修改位置
        String cdwz = (String) jsonRequest.getReqData().get("cdwz");
        if (null != cdwz && !"".equals(cdwz)) {
            glGnRepository.updatePidById(id, cdwz);
        }
        return jsonResponse;
    }

    //修改菜单位置
    @Transactional
    public JsonResponse updateGnLj(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        String id = (String) jsonRequest.getReqData().get("id");
        String pid = (String) jsonRequest.getReqData().get("pid");
        glGnRepository.updatePidById(id, pid);
        return jsonResponse;
    }
}
